package com.sport.setting.repository;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sport.model.application.dtos.SportMemberUpdateDto;
import com.sport.model.application.pojos.SportMemberEnroll;
import com.sport.model.common.dtos.ResponseResult;
import com.sport.model.common.enums.AppHttpCodeEnum;
import com.sport.model.setting.dtos.SportNumberParamDto;
import com.sport.model.setting.dtos.SportNumberShowDto;
import com.sport.model.setting.vos.CountVo;
import com.sport.model.user.bos.AccountUserBo;
import com.sport.setting.mapper.SportMemberEnrollMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Component
@Transactional
public class SportMemberEnrollRepository extends ServiceImpl<SportMemberEnrollMapper, SportMemberEnroll> {

    private SportMemberEnrollMapper sportMemberEnrollMapper;

    public SportMemberEnrollRepository(@Autowired SportMemberEnrollMapper sportMemberEnrollMapper) {
        this.sportMemberEnrollMapper = sportMemberEnrollMapper;
    }

    public Map<String, List<String>> showGradeAndUnit() {
        List<Object> gradeList=new ArrayList<>();
        Map<String,List<String>> gradeUnitMap=new HashMap<>();
        List<Map<String, Object>> list = sportMemberEnrollMapper.selectMaps(Wrappers.<SportMemberEnroll>lambdaQuery()
                .select(SportMemberEnroll::getSportMemberGrade,SportMemberEnroll::getSportMemberUnit)
                .eq(SportMemberEnroll::getSchoolTenementId, AccountUserBo.getUser().getSchoolTenementId())
                .eq(SportMemberEnroll::getSportBindingSchool, AccountUserBo.getUser().getSportId())
//                .groupBy(SportMemberEnroll::getSportMemberGrade)
                );

        for (Map<String,Object> map : list){
            String sportMemberGrade = map.get("sport_member_grade").toString();
            String sportMemberUnit = map.get("sport_member_unit").toString();
            if(!gradeList.contains(sportMemberGrade)){
                gradeList.add(sportMemberGrade);
                gradeUnitMap.put(sportMemberGrade,new ArrayList<>());
            }
            if(!gradeUnitMap.get(sportMemberGrade).contains(sportMemberUnit)) {
                gradeUnitMap.get(sportMemberGrade).add(sportMemberUnit);
            }
        }
        return gradeUnitMap;
    }

    public ResponseResult showQuantity(SportNumberShowDto sportNumberShowDto) {
        Integer maleCount =0;
        Integer femaleCount=0;
        List<SportMemberEnroll> maleList=null;
        List<SportMemberEnroll> femaleList=null;
        String schoolTenementId = sportNumberShowDto.getAccountUserBo().getSchoolTenementId();
        if(sportNumberShowDto.getSportMemberUnit()==null){
            //查询该年级中男生人数
             maleList = sportMemberEnrollMapper.selectList(Wrappers.<SportMemberEnroll>lambdaQuery()
                    .eq(SportMemberEnroll::getSportMemberGrade, sportNumberShowDto.getSportMemberGrade())
                     .eq(SportMemberEnroll::getSchoolTenementId,schoolTenementId)
                    .eq(SportMemberEnroll::getSportMemberGender, 1));
            maleCount=maleList.size();
            //查询该年级中女生人数
            femaleList = sportMemberEnrollMapper.selectList(Wrappers.<SportMemberEnroll>lambdaQuery()
                    .eq(SportMemberEnroll::getSportMemberGrade, sportNumberShowDto.getSportMemberGrade())
                    .eq(SportMemberEnroll::getSchoolTenementId,schoolTenementId)
                    .eq(SportMemberEnroll::getSportMemberGender, 2));
            femaleCount =femaleList.size();
        }else {
            //查询该年级该单位中男生人数
            maleList = sportMemberEnrollMapper.selectList(Wrappers.<SportMemberEnroll>lambdaQuery()
                    .eq(SportMemberEnroll::getSportMemberGrade, sportNumberShowDto.getSportMemberGrade())
                    .eq(SportMemberEnroll::getSportMemberUnit, sportNumberShowDto.getSportMemberUnit())
                    .eq(SportMemberEnroll::getSchoolTenementId,schoolTenementId)
                    .eq(SportMemberEnroll::getSportMemberGender, 1));
            maleCount=maleList.size();
            //查询该年级该单位中女生人数
            femaleList = sportMemberEnrollMapper.selectList(Wrappers.<SportMemberEnroll>lambdaQuery()
                    .eq(SportMemberEnroll::getSportMemberGrade, sportNumberShowDto.getSportMemberGrade())
                    .eq(SportMemberEnroll::getSportMemberUnit, sportNumberShowDto.getSportMemberUnit())
                    .eq(SportMemberEnroll::getSchoolTenementId,schoolTenementId)
                    .eq(SportMemberEnroll::getSportMemberGender, 2));
            femaleCount =femaleList.size();
        }
        CountVo countVo = new CountVo();
        countVo.setMaleCount(maleCount);
        countVo.setFemaleCount(femaleCount);
        countVo.setMaleList(maleList);
        countVo.setFemaleList(femaleList);
        return ResponseResult.okResult(countVo);
    }

    public ResponseResult assignedNumber(SportNumberParamDto sportNumberParamDto) {

        String schoolTenementId = sportNumberParamDto.getAccountUserBo().getSchoolTenementId();
        //设置男生号码
        boolean male = setNumber(sportNumberParamDto.getMaleStartNumber(), sportNumberParamDto.getMaleList(), sportNumberParamDto.getMaleCountBy(),
                sportNumberParamDto.getMaleEndNumber(),schoolTenementId);

        //设置女生号码
        boolean female = setNumber(sportNumberParamDto.getFemaleStartNumber(), sportNumberParamDto.getFemaleList(), sportNumberParamDto.getFemaleCountBy(),
                sportNumberParamDto.getFemaleEndNumber(),schoolTenementId);
        if(male&&female){
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }

    private boolean setNumber(int startNumber,List<SportMemberEnroll> list,int countBy,int endNumber,String schoolTenementId) {
        for (SportMemberEnroll sportMemberEnroll : list) {
            sportMemberEnroll.setSportMemberNum(String.valueOf(startNumber));
            sportMemberEnrollMapper.update(sportMemberEnroll,Wrappers.<SportMemberEnroll>lambdaUpdate()
                    .eq(SportMemberEnroll::getId,sportMemberEnroll.getId())
                    .eq(SportMemberEnroll::getSchoolTenementId,schoolTenementId));
            startNumber+= countBy;
            //如果分配的号码大于截止号码,停止分配
            if(endNumber<startNumber){
                break;
            }
        }
        return true;
    }

    public List<SportMemberEnroll> getMemberList(String unit, int gender) {

        List<SportMemberEnroll> memberEnrollList = sportMemberEnrollMapper.selectList(Wrappers.<SportMemberEnroll>lambdaQuery()
                .eq(SportMemberEnroll::getSportMemberUnit, unit)
                .eq(SportMemberEnroll::getSchoolTenementId, AccountUserBo.getUser().getSchoolTenementId())
                .eq(SportMemberEnroll::getSportBindingSchool, AccountUserBo.getUser().getSportId())
                .eq(SportMemberEnroll::getSportMemberGender,gender));
        return memberEnrollList;
    }

    public boolean updateEnrollItem(SportMemberUpdateDto sportMemberUpdateDto) {
        SportMemberEnroll sportMemberEnroll = new SportMemberEnroll();
        sportMemberEnroll.setId(sportMemberUpdateDto.getId());
        List<String> memberProjectList = sportMemberUpdateDto.getMemberProjectList();
        StringBuilder stringBuilder=new StringBuilder();
        for (int i=0;i<memberProjectList.size();i++){
            if(i==0){
                stringBuilder.append(memberProjectList.get(i));
            }else{
                stringBuilder.append(",");
                stringBuilder.append(memberProjectList.get(i));
            }
        }
        sportMemberEnroll.setSportMemberProject(stringBuilder.toString());

        int i = sportMemberEnrollMapper.updateById(sportMemberEnroll);
        if(i>0){
            return true;
        }
        return false;
    }

    public List<SportMemberEnroll> getMemberByIds(String[] ids){
        List<Long> record = new ArrayList<>();
        for (int i = 0; i < ids.length; ++i)
            record.add(Long.valueOf(ids[i]));

        return sportMemberEnrollMapper.selectBatchIds(record);
    }
}
